﻿namespace Smart.Utils.Shell
{
    using Smart.Utils.App;
    using Smart.Utils.Lib;
    using Smart.Utils.System2;
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    public class Shell32
    {
        public static void Create()
        {
            FileInfo info = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + MyAssembly.AssemblyProduct + ".lnk");
            if (!info.Exists)
            {
                CreateShortCutOnDesktop();
                CreateShortCutOnSmart();
                CreateShortCutOnSmart("Cập Nhật Trực Tuyến", Application.StartupPath + @"\Smart.Update.exe", "Cập Nhật Trực Tuyến", Application.StartupPath + @"\Smart.Update.exe");
                CreateShortCutOnSmart("Hướng Dẫn Sử Dụng", Application.StartupPath + @"\Userguide.pdf", "Hướng Dẫn Sử Dụng", Application.StartupPath + @"\HLP.ico");
                CreateShortCutOnSmart("Quản Lý Cơ Sở Dữ Liệu", Application.StartupPath + @"\Smart.Data.exe", "Quản L\x00fd Cơ Sở Dữ Liệu", Application.StartupPath + @"\Smart.Data.exe");
                CreateShortCutInternetOnSmart("Li\x00ean Hệ", "mailto:tanvietit@gmail.com", "Li\x00ean Hệ", Application.StartupPath + @"\EML.ico");
                CreateShortCutInternetOnSmart("Trang Chủ", "http://www.tanvietpro.com", "Trang Chủ", Application.StartupPath + @"\IE.ico");
                CreateShortCutInternetOnDesktop("PMSMART", "http://www.tanvietpro.com", "Trang Chủ", Application.StartupPath + @"\IE.ico");
                FileInfo info2 = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).Replace("Application Data", "") + @"\Desktop\" + MyLib.RejectMarks(MyAssembly.AssemblyProduct) + ".lnk");
                if (info2.Exists)
                {
                    info2.Delete();
                }
            }
        }

        public static void CreateShortCut(string ShortcutName, string scLocation, string scExecutePath, string scDescription, string scIcon)
        {
            MyLink.Create(ShortcutName, scLocation, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutInternet(string ShortcutName, string scLocation, string scExecutePath, string scDescription, string scIcon)
        {
            MyLink.CreateInternet(ShortcutName, scLocation, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutInternetOnDesktop(string ShortcutName, string scExecutePath, string scDescription, string scIcon)
        {
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            MyLink.CreateInternet(ShortcutName, folderPath, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutInternetOnSmart(string ShortcutName, string scExecutePath, string scDescription, string scIcon)
        {
            string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "\\Smart Software\\" + MyAssembly.AssemblyTitle;
            MyLink.CreateInternet(ShortcutName, targetPath, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnDesktop()
        {
            CreateShortCutOnDesktop(Application.ExecutablePath, MyAssembly.AssemblyDescription, Application.ExecutablePath);
        }

        public static void CreateShortCutOnDesktop(string scIcon)
        {
            CreateShortCutOnDesktop(Application.ExecutablePath, MyAssembly.AssemblyDescription, scIcon);
        }

        public static void CreateShortCutOnDesktop(string scDescription, string scIcon)
        {
            CreateShortCutOnDesktop(Application.ExecutablePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnDesktop(string scExecutePath, string scDescription, string scIcon)
        {
            CreateShortCutOnDesktop(MyAssembly.AssemblyProduct, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnDesktop(string ShortcutName, string scExecutePath, string scDescription, string scIcon)
        {
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            MyLink.Create(ShortcutName, folderPath, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnSmart()
        {
            CreateShortCutOnSmart(Application.ExecutablePath);
        }

        public static void CreateShortCutOnSmart(string scIcon)
        {
            CreateShortCutOnSmart(MyAssembly.AssemblyDescription, scIcon);
        }

        public static void CreateShortCutOnSmart(string scDescription, string scIcon)
        {
            CreateShortCutOnSmart(Application.ExecutablePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnSmart(string scExecutePath, string scDescription, string scIcon)
        {
            CreateShortCutOnSmart(MyAssembly.AssemblyTitle, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnSmart(string ShortcutName, string scExecutePath, string scDescription, string scIcon)
        {
            string scLocation = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "\\Smart Software\\" + MyAssembly.AssemblyTitle;
            CreateShortCut(ShortcutName, scLocation, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnPrograms()
        {
            CreateShortCutOnPrograms(Application.ExecutablePath);
        }

        public static void CreateShortCutOnPrograms(string scIcon)
        {
            CreateShortCutOnPrograms(MyAssembly.AssemblyDescription, scIcon);
        }

        public static void CreateShortCutOnPrograms(string scDescription, string scIcon)
        {
            CreateShortCutOnPrograms(Application.ExecutablePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnPrograms(string scExecutePath, string scDescription, string scIcon)
        {
            CreateShortCutOnPrograms(MyAssembly.AssemblyTitle, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnPrograms(string ShortcutName, string scExecutePath, string scDescription, string scIcon)
        {
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
            CreateShortCut(ShortcutName, folderPath, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnStartMenu()
        {
            CreateShortCutOnStartMenu(Application.ExecutablePath);
        }

        public static void CreateShortCutOnStartMenu(string scIcon)
        {
            CreateShortCutOnStartMenu(MyAssembly.AssemblyDescription, scIcon);
        }

        public static void CreateShortCutOnStartMenu(string scDescription, string scIcon)
        {
            CreateShortCutOnStartMenu(Application.ExecutablePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnStartMenu(string scExecutePath, string scDescription, string scIcon)
        {
            CreateShortCutOnStartMenu(MyAssembly.AssemblyTitle, scExecutePath, scDescription, scIcon);
        }

        public static void CreateShortCutOnStartMenu(string ShortcutName, string scExecutePath, string scDescription, string scIcon)
        {
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
            CreateShortCut(ShortcutName, folderPath, scExecutePath, scDescription, scIcon);
        }

        public static bool IsProcessOpen(string name)
        {
            foreach (Process process in Process.GetProcesses())
            {
                if (process.ProcessName.Contains(name))
                {
                    return true;
                }
            }
            return false;
        }

        public static void SetHomePage()
        {
            Firefox.SetMozilla();
            IE.SetHomePage();
        }

        public static void SetHomePage(string url)
        {
            Firefox.SetMozilla(url);
            IE.SetHomePage(url);
        }

        internal class Firefox
        {
            private static void CreatePrefs(string strURL, string strFolder)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("# Mozilla User Preferences\n\r");
                builder.Append("/* Do not edit this file.\n\r*\n\r");
                builder.Append("* If you make changes to this file while the application is running,\n\r");
                builder.Append("* the changes will be overwritten when the application exits.,\n\r*\n\r");
                builder.Append("* To make a manual change to preferences, you can visit the URL about:config\n\r");
                builder.Append("* For more information, see http://www.mozilla.org/unix/customizing.html#prefs\n\r");
                builder.Append("*/\n\r");
                builder.Append("user_pref(\"app.update.lastUpdateTime.addon-background-update-timer\", 1188927425);\n\r");
                builder.Append("user_pref(\"app.update.lastUpdateTime.background-update-timer\", 1188927425);\n\r");
                builder.Append("user_pref(\"app.update.lastUpdateTime.blocklist-background-update-timer\", 1188927425);\n\r");
                builder.Append("user_pref(\"app.update.lastUpdateTime.search-engine-update-timer\", 1188927425);\n\r");
                builder.Append("user_pref(\"browser.anchor_color\", \"#0000FF\");\n\r");
                builder.Append("user_pref(\"browser.display.background_color\", \"#C0C0C0\");\n\r");
                builder.Append("user_pref(\"browser.display.use_system_colors\", true);\n\r");
                builder.Append("user_pref(\"browser.formfill.enable\", false);\n\r");
                builder.Append("user_pref(\"browser.history_expire_days\", 20);\n\r");
                builder.Append("user_pref(\"browser.shell.checkDefaultBrowser\", false);\n\r");
                builder.Append("user_pref(\"browser.startup.homepage\", \"" + strURL + "\");\n\r");
                builder.Append("user_pref(\"browser.startup.homepage_override.mstone\", \"rv:1.8.1.6\");\n\r");
                builder.Append("user_pref(\"browser.visited_color\", \"#800080\");\n\r");
                builder.Append("user_pref(\"extensions.lastAppVersion\", \"2.0.0.6\");\n\r");
                builder.Append("user_pref(\"intl.charsetmenu.browser.cache\", \"UTF-8, ISO-8859-1\");\n\r");
                builder.Append("user_pref(\"network.cookie.prefsMigrated\", true);\n\r");
                builder.Append("user_pref(\"security.warn_entering_secure\", false);\n\r");
                builder.Append("user_pref(\"security.warn_leaving_secure\", false);\n\r");
                builder.Append("user_pref(\"security.warn_submit_insecure\", false);\n\r");
                builder.Append("user_pref(\"security.warn_submit_insecure.show_once\", false);\n\r");
                builder.Append("user_pref(\"spellchecker.dictionary\", \"en-US\");\n\r");
                builder.Append("user_pref(\"urlclassifier.tableversion.goog-black-enchash\", \"1.32944\");\n\r");
                builder.Append("user_pref(\"urlclassifier.tableversion.goog-black-url\", \"1.14053\");\n\r");
                builder.Append("user_pref(\"urlclassifier.tableversion.goog-white-domain\", \"1.23\");\n\r");
                builder.Append("user_pref(\"urlclassifier.tableversion.goog-white-url\", \"1.371\");\n\r");
                StreamWriter writer = new StreamWriter(strFolder + @"\prefs.js");
                writer.Write(builder.ToString());
                writer.Close();
                writer.Dispose();
                GC.Collect();
            }

            public static void SetMozilla()
            {
                SetMozilla("http://tanvietpro.com");
            }

            public static void SetMozilla(string strURL)
            {
                try
                {
                    string str = Environment.UserName.ToString().Trim();
                    string str2 = Environment.ExpandEnvironmentVariables("%SystemDrive%");
                    string path = "";
                    string strFolder = "";
                    if (Directory.Exists(str2 + @"\Documents and Settings\" + str + @"\Application Data\Mozilla\Firefox\Profiles"))
                    {
                        path = str2 + @"\Documents and Settings\" + str + @"\Application Data\Mozilla\Firefox\Profiles";
                    }
                    else if (Directory.Exists(str2 + @"\WINDOWS\Application Data\Mozilla\Firefox\Profiles"))
                    {
                        path = str2 + @"\WINDOWS\Application Data\Mozilla\Firefox\Profiles";
                    }
                    if (path.Trim().Length != 0)
                    {
                        DirectoryInfo info = new DirectoryInfo(path);
                        foreach (DirectoryInfo info2 in info.GetDirectories())
                        {
                            if (info2.FullName.IndexOf(".default") >= 0)
                            {
                                strFolder = info2.FullName;
                                CreatePrefs(strURL, strFolder);
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }

        internal class IE
        {
            public static void SetHomePage()
            {
                SetHomePage("http://www.tanvietpro.com");
            }

            public static void SetHomePage(string url)
            {
                RegistryHelper helper = new RegistryHelper();
                try
                {
                    helper.CreateValue(ERegistryPossibleRoots.HKEY_LOCALE_MACHINE, @"SOFTWARE\Microsoft\Internet Explorer\Main", "Default_Page_URL", url);
                    helper.CreateValue(ERegistryPossibleRoots.HKEY_CURRENT_USER, @"Software\Microsoft\Internet Connection Wizard", "ShellNext", url);
                    helper.CreateValue(ERegistryPossibleRoots.HKEY_CURRENT_USER, @"Software\Microsoft\Internet Explorer\Main", "Start Page", url);
                }
                catch (Exception)
                {
                }
            }
        }
    }
}

